<?php

class ElFinderWidget extends CInputWidget {

    public $assets;
    public $folder;

    public function init() {
        parent::run();
        $assetObj = Yii::app()->getAssetManager();
        $assetObj->newFileMode = 0644; //fix bug on Matbao hosting
        $assetObj->newDirMode = 0755; //fix bug on Matbao hosting
        
        $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
        $this->assets = $assetObj->publish($path);
    }

    public function run() {
        //set $_SESSION
        $_SESSION['file_manager'] = Yii::app()->params['file_manager'];
        
        //register Css & Script
        $cs = Yii::app()->getClientScript();
        $cssFiles = array(
            'elfinder.min.css',
            'jquery-ui.css',
            'theme.css',
        );
        foreach ($cssFiles as $file) {
            echo '<link rel="stylesheet" type="text/css" media="all" href="'
            . $this->assets . '/css/' . $file .'"/>';
        }
        
        $cs->registerScriptFile($this->assets . '/js/elfinder.min.js', CClientScript::POS_BEGIN);
        $cs->registerScript('file-manager', "
$().ready(function() {
    var elf = $('#elfinder').elfinder({
        url : '$this->assets/php/connector.php'
    }).elfinder('instance');
});", CClientScript::POS_END);
        
        echo '<div id="elfinder"></div>';
    }
    
}